﻿using MoetutuServer.Helper;
using MoetutuServer.Helper.RepairHelper;
using MoetutuServer.PublishHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoetutuServer.RepairHelper
{
    class StartScanner
    {
        public static void AnalysisContent(String UserDataPath, int StartPage, int EndPage)
        {
            foreach (string Id in GetArticleIdList(UserDataPath, StartPage, EndPage))
            {
                ModifyContent.ModifySingle(UserDataPath, Id);
            }
        }
        /// <summary>
        /// 读取文章ID列表
        /// </summary>
        /// <param name="UserDataPath"></param>
        /// <param name="StartPage"></param>
        /// <param name="EndPage"></param>
        /// <returns></returns>
        public static List<string> GetArticleIdList(String UserDataPath, int StartPage, int EndPage)
        {

            List<string> TempList = new List<string>();
            String Link = "https://pic.moetutu.com/page/{0}";
            Console.WriteLine("起始{0}，结束{1}", StartPage, EndPage);
            if (StartPage < 1)
                StartPage = 1;

            for (int i = StartPage; i < EndPage; i++)
            {
                String TempLink = string.Format(Link, i);

                string Shtml = WebHelper.HttpGet(TempLink);
                foreach (string line in HtmlHelper.GetLinkVlaueList(Shtml, "//a[@class='thumb-holder-link']"))
                {
                    Console.WriteLine("第{0}页{1}", i, line);
                    string Id = line.Replace("https://pic.moetutu.com/", "");
                    Console.WriteLine("验证文章：" + Id);
                    TempList.Add(Id);
                 
                }
            }
            return TempList;
        }

        internal static void RepairTag(String UserDataPath, int StartPage, int EndPage)
        {
            foreach (string Id in GetArticleIdList(UserDataPath, StartPage, EndPage))
            {
                ModifyContent.RepairForTag(UserDataPath, Id);
            }
        }

        internal static void ReplaceText(String UserDataPath, int StartPage, int EndPage, string OldTextRule, string NewTxet)
        {
            foreach (string Id in GetArticleIdList(UserDataPath, StartPage, EndPage))
            {
                ModifyContent.ReplaceSingle(UserDataPath, Id,  OldTextRule,  NewTxet);
            }
        }
    }
}
